qui clear frames
qui use "data/ready4analysis.dta",clear

****************************************************************************************
**** FIGURE 1
****************************************************************************************

* Covariates for main model and model in supporting information
local covars_main "i.female c.age i.education i.year" 

foreach dv in z { // Change to "z a b c d e f" for individual items
		foreach mod in main { // Optional for alternative covariates
		foreach t in 90 { // Change to 90 for more strict class classification
		foreach sample in all { // Change to fullpan to limit analysis to only those with more than 8 employment observations
		
		di "`dv'-`mod'-`t'-`sample'"
		qui eststo clear	

		local ifcond_all "!mi(temp5) & !mi(red`dv')" // Baseline conditions for being included in sample
		local ifcond_fullpan "!mi(temp5) & !mi(red`dv') & N_egp_indresp_emp>8" // Baseline conditions for being included in sample
	
		qui cap drop temp5
		qui recode egp_indresp (60/120=.), gen(temp5)
		qui eststo `dv'_egp`mod'`t'_`sample'2: reg red`dv' ib1.temp5 `covars_`mod'' if `ifcond_`sample'', vce(cluster pidp)
			qui estimates store `dv'_egp_xs_`mod'_`sample'
			qui gen `dv'_egp_xssample_`mod'_`t'_`sample'=1 if e(sample)

		qui drop temp5	
		qui recode egp_panel`t' (60/120=.), gen(temp5)
		
		qui eststo `dv'_egp`mod'`t'_`sample'3: reg red`dv' ib1.temp5 `covars_`mod'' if `ifcond_`sample'', vce(cluster pidp)
			qui estimates store `dv'_egp_panel_`mod'_`t'_`sample'
			qui gen `dv'_egp_panel_`mod'_`t'_`sample'=1 if e(sample)
		
		qui drop temp5
		qui recode egp_lifetime`t' (60/120=.), gen(temp5)

		qui eststo `dv'_egp`mod'`t'_`sample'4: reg red`dv' ib1.temp5 `covars_`mod'' if `ifcond_`sample'', vce(cluster pidp)
			qui estimates store `dv'_egp_life_`mod'_`t'_`sample'
			qui gen `dv'_egp_life_`mod'_`t'_`sample'=1 if e(sample)
		
		qui drop temp5
		qui recode p_egp (60/120=.), gen(temp5)
		
		qui eststo `dv'_egp`mod'`t'_`sample'1: reg red`dv' ib1.temp5 `covars_`mod'' if `ifcond_`sample'', vce(cluster pidp)
			qui estimates store `dv'_egp_father_`mod'_`sample'
			qui gen `dv'_egp_father_`mod'_`t'_`sample'=1 if e(sample)

		qui save "temp/fig1_data_`dv'_egp_`mod'_`t'_`sample'.dta",replace
		
		
		*Create table with models
		qui esttab `dv'_egp`mod'`t'_`sample'1 `dv'_egp`mod'`t'_`sample'2 `dv'_egp`mod'`t'_`sample'3 `dv'_egp`mod'`t'_`sample'4  using "tables/figure1_`dv'_egp_`mod'_`t'_`sample'.rtf", replace b(3) se(3) varwidth(25)  aic modelwidth(12) scalars(N r2) nonumbers mtitles("Father's class'" "Current class" "Panel class" "Lifetime class") label nobaselevels compress refcat(2.temp5 "Class (Ref category: Workers):" 1.female "Controls:" age "Age" 1993.year "Year (Ref category: 1991):", nolabel) 

		qui cap graph drop Graph
		qui coefplot (`dv'_egp_father_`mod'_`sample', label(Father's class)) (`dv'_egp_xs_`mod'_`sample', label(Current class)) (`dv'_egp_panel_`mod'_`t'_`sample',label(Stable class during BHPS)) (`dv'_egp_life_`mod'_`t'_`sample',label(Stable class over lifetime)), keep(*.temp5)  xline(0) base title("Class and Core Values, using different definitions of class and DV (DV: `dv' Model: `mod', class: egp, threshold: `t', sample: _`sample')", size(medium)) legend(r(4) position(4)) generate(fig1_`dv'_egp_`mod'_`t'_`sample'_)
		qui graph export "temp/fig1_`dv'_egp_`mod'_`t'_`sample'.png", as(png) name("Graph") width(2400) replace

		* Below code is for R figs
		qui cap frame drop fig1
		qui frame copy default fig1
		qui frame change fig1
			qui keep fig1_`dv'_egp_`mod'_`t'_`sample'*
			qui drop if mi(fig1_`dv'_egp_`mod'_`t'_`sample'_by)
			qui decode fig1_`dv'_egp_`mod'_`t'_`sample'_plot, gen(fig1_`dv'_egp_`mod'_`t'_`sample'_plotS)
			
			qui gen class=1
			qui replace class=2 if inrange(_n,5,8)
			qui replace class=3 if inrange(_n,9,12)
			qui replace class=4 if inrange(_n,13,16)
			qui replace class=5 if inrange(_n,17,20)
			
			qui rename fig1_`dv'_egp_`mod'_`t'_`sample'_* *
			qui save "figs/fig1_`dv'_egp_`mod'_`t'_`sample'.dta",replace

		qui frame change default

		}
		}
	}
}



